#version 460
#extension GL_EXT_ray_tracing : require
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_buffer_reference2 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require


//#include "ray_std.h"
#include "../../../../core/shader_binding_ID.h"
#include "../../../../core/shader_ob.h"



layout(binding = DEF_RenderFrameBindID, set = 0, scalar) uniform FrameProp {
	GLSL_RenderFrame renderFrame;
};



layout(location = 0) rayPayloadInEXT S_Ray hitValue;
layout(location = 3) callableDataEXT S_SceneDataCall callSceneData;

void main() {
    //hitValue.Color = vec3( max(0,dot( gl_WorldRayDirectionEXT, normalize(vec3(1, 1, 1)) )) ) + 0.1;
    //ligth *= 0.5;
	//gl_WorldRayDirectionEXT;
	
	
	switch(hitValue.RayType){
		//来自普通方式射线
		case DEF_LigthRayType:
		case DEF_RayType : {
			if ( renderFrame.m_world_ShaderID >= 0 ) {
				callSceneData.rayDir = gl_WorldRayDirectionEXT;
				callSceneData.MaterialID = 0;
				callSceneData.PrimitiveIndex = 0;
				callSceneData.InstanceIndex = 0;
				callSceneData.Vert = gl_WorldRayDirectionEXT;
				callSceneData.ligthDir = gl_WorldRayDirectionEXT;
				callSceneData.Normal = -gl_WorldRayDirectionEXT;
				callSceneData.Color = vec3(0);


				executeCallableEXT(renderFrame.m_world_ShaderID, 3);
				
				vec3 w = vec3(0.2125,0.7154,0.0721);
				float luminance = max(dot(callSceneData.Color, w), 0);
				hitValue.Color += callSceneData.Color;
				hitValue.irradiance += luminance;
				
				//if(renderFrame.m_world_ShaderID != 2){
				//	hitValue.Color = vec3(0,0,1);
				//}
				//float ligth = max( dot(gl_WorldRayDirectionEXT, normalize(vec3(0, 1, 0)) ), 0);
				//hitValue.Color += vec3(abs(gl_WorldRayDirectionEXT)) * (ligth);
				//hitValue.irradiance += ligth;
				//hitValue.LigthMask = 0xffffffff;
			}
			else {
				float ligth = max( dot(gl_WorldRayDirectionEXT, normalize(vec3(0, 1, 0)) ) + 1.0, 0);
				//hitValue.Color += vec3(abs(gl_WorldRayDirectionEXT)) * (ligth);
				hitValue.Color += ligth*0.5;
				hitValue.irradiance += ligth;
				hitValue.LigthMask = 0xffffffff;
			}
			

			//float ligth = max( dot(gl_WorldRayDirectionEXT, normalize(vec3(0, 1, 0)) ), 0);
			//hitValue.Color += vec3(abs(gl_WorldRayDirectionEXT)) * (ligth);
			//hitValue.irradiance += ligth;
			//hitValue.LigthMask = 0xffffffff;

			
			hitValue.LigthDis += 1;
			hitValue.Depth = 64;
			return;
		}

		//计算灯光
		case DEF_LigthReturnRayType : {
			//hitValue.Color += vec3(0, 0, 1);
			
			R_GlobalParameter_LigthPoint LigthPoint_GParam = R_GlobalParameter_LigthPoint(gGlobalParam.m_AddressLigthPoint);
			R_GlobalParameter_LigthArea LigthArea_GParam = R_GlobalParameter_LigthArea(gGlobalParam.m_AddressLigthArea);
			R_GlobalParameter_LigthSpot LigthSpot_GParam = R_GlobalParameter_LigthSpot(gGlobalParam.m_AddressLigthSpot);

			uint Ligth_Type = hitValue.Ligth_Type_ID >> 16;
			switch(Ligth_Type){
				case DEF_ObType_Ligth_Point:{
					S_LigthPoint light = LigthPoint_GParam.LigthPointPtr[ hitValue.Ligth_Type_ID&0x0000ffff ];
					//gl_WorldRayOriginEXT;
					if(hitValue.Dis < light.m_falloff && light.m_enable != 0){
						//hitValue.irradiance += hitValue.Dis / light.m_falloff;
						float dt = max(dot(hitValue.Normal, gl_WorldRayDirectionEXT), 0);
						dt = (light.m_falloff - hitValue.Dis) * dt;

						vec3 w = vec3(0.2125,0.7154,0.0721);
						float luminance = (dot(w, (hitValue.Color)));
						//hitValue.Color += vec3(dt) * light.m_intensity * (hitValue.irradiance) * 0.5;
						hitValue.Color += vec3(dt) * light.m_intensity * 0.5 / (1+hitValue.Dis);
						//hitValue.Color *= hitValue.irradiance;
						//hitValue.Color = mix(hitValue.Color, vec3(dt) * light.m_intensity, 0.5);
						//hitValue.Color = vec3(0.0001);
						//hitValue.irradiance *= dt;
					}else{

					}
					break;
				}
			}
			//hitValue.Dis += max(0, dot(hitValue.Dir, hitValue.Normal)) * LigthParallel.L[0].m_intensity * 0.4;
			++hitValue.Depth;
			return;
		}
	}
	//hitValue.Dis = 64;	
	hitValue.Color = vec3(1);
}